import {app, ServerInfo, Session} from "mydog";
import {randArr, randInt} from "./util";

export const enum svrType {
    "gate" = "gate",
    "connector" = "connector",
    "info" = "info",
    "game" = "game",
}



let infoSvrs = app.serversConfig[svrType.info];
export function getInfoId(uid: number) {
    return infoSvrs[uid % infoSvrs.length].id;
}


export const enum constKey {
    "gate" = "gate",
}

export function randGameSvrByType(gameType:GameType):ServerInfo|null{
    let svrInfos = app.getServersByType(svrType.game)
    let arr = [];
    for(let i=0;i<svrInfos.length;i++){
        if(svrInfos[i].gameType == gameType){
            arr.push(svrInfos[i])
        }
    }
    if(arr.length==0){
        return null;
    }
    return randArr(arr);
}

export function changeInfoGameState(uid: number, gameState: I_gameState) {
    app.rpc(getInfoId(uid)).info.main.setGameState(uid, gameState);
}

export function changeConGameState(uid: number, sid: string, gameState: I_gameState) {
    app.rpc(sid).connector.main.setGameState(uid, gameState);
}

/**
 * 从session里获取游戏状态
 * @param session 
 */
export function getGameState(session: Session): I_gameState {
    return session.get("gameState");
}

export enum GameType {
    NULL="",
    SK="sk", //双扣
    NB_DDZ="nbddz", //宁波斗地主
}

export interface I_gameState {
    "gameSvr": string,
    "gameType": GameType,
    "tableId": number
}